/**
 * Created with IntelliJ IDEA.
 * User: 卞京帅{bjslogin@gmail.com}
 * Date: 12-11-20
 * Time: 下午11:18
 * To change this template use File | Settings | File Templates.
 */
Ext.define('Airways.controller.route.ConPortController', {
    extend:'Ext.app.Controller',
    models:['route.ExpAirPort','route.ExpCountry'],
    stores:['route.ExpAirPortStore','route.ExpCountryStore'],
    views:['route.ConPortView'],
    refs:[
        {ref:'conPortLeftGrid',selector:'conPortLeftGrid'},
        {ref:'conPortRightGrid',selector:'conPortRightGrid'},
        {ref:'conPortCountryWin',selector:'conPortCountryWin'},
        {ref:'conPortPortWin',selector:'conPortPortWin'}
    ],
    init:function(){
        this.control({
            'conPortLeftGrid':{
                afterrender:function(){
                    var store = this.getRouteExpCountryStoreStore();
                    var countryName = this.getConPortLeftGrid().down("textfield[name=countryName]");
                    var countryCode = this.getConPortLeftGrid().down("textfield[name=countryCode]");
                    store.on('beforeload',function(){
                        store.proxy.extraParams = {
                            countryName:countryName.getValue(),
                            countryCode:countryCode.getValue()
                        }
                    });
                },
                itemclick:function(view,record){
                    this.countryId = record.get('countryId');
                }
            },
            'conPortRightGrid':{
                afterrender:function(){
                    var controller = this;
                    var store = controller.getConPortRightGrid().getStore();
                    var portName = controller.getConPortRightGrid().down("textfield[name=portName]");
                    var portCode = controller.getConPortRightGrid().down("textfield[name=portCode]");
                    store.on('beforeload',function(){
                        store.proxy.extraParams = {
                            portName:portName.getValue(),
                            portCode:portCode.getValue(),
                            countryId:controller.countryId
                        }
                    });
                }
            },
            'conPortLeftGrid button[action=find]':{
                click:function(){
                    this.getConPortLeftGrid().getStore().loadPage(1);
                }
            },
            'conPortLeftGrid button[action=create]':{
                click:function(b){
                    var win = this.getConPortCountryWin();
                    if(win == undefined){
                        win = Ext.create('Airways.view.route.ConPortCountryWin');
                    }
                    win.show(b);
                    win.down('form').getForm().reset();
                }
            },
            'conPortLeftGrid button[action=update]':{
                click:function(b){
                    var selection = this.getConPortLeftGrid().getSelectionModel().getSelection();
                    if(selection.length == 0){
                        Ext.MessageBox.show({title:'提示',msg:'请选择要修改的记录',buttons: Ext.MessageBox.OK,icon:Ext.MessageBox.ERROR});
                        return;
                    }
                    var win = this.getConPortCountryWin();
                    if(win == undefined){
                        win = Ext.create('Airways.view.route.ConPortCountryWin');
                    }
                    win.show(b);
                    win.down('form').getForm().loadRecord(selection[0]);
                }
            },
            'conPortLeftGrid button[action=delete]':{
                click:function(){
                    var selection = this.getConPortLeftGrid().getSelectionModel().getSelection();
                    if(selection.length == 0){
                        Ext.MessageBox.show({title:'提示',msg:'请选择要修改的记录',buttons: Ext.MessageBox.OK,icon:Ext.MessageBox.ERROR});
                        return;
                    }
                    alert('todo')
                }
            },
            'conPortCountryWin button[action=save]':{
                click:function(){
                    var store = this.getConPortLeftGrid().getStore();
                    var win = this.getConPortCountryWin();
                    var data = win.down('form').getForm().getValues();
                    callAjax(basePath+'port/saveCountry',{data:Ext.JSON.encode(data)},function(){
                        win.hide();
                        store.loadPage(1);
                    })
                }
            },
            'conPortCountryWin button[action=close]':{
                click:function(){
                    this.getConPortCountryWin().close();
                }
            }
        })
    }
});
